﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;
using DG.Tweening;
using UnityEngine.UI;
using System;

public class PuzzleDragHelperElement : MonoBehaviour, IDragHandler, IBeginDragHandler, IEndDragHandler, IPointerDownHandler
{
    public int id;

    public int GroupId = -1;
    public int groupId
    {
        set
        {
            text.text = value.ToString();
            GroupId = value;
        }
        get
        {
            return GroupId;
        }
    }
    public int anchorId = -1;
    public float order;
    public bool inGrid = false;
    public bool inDrag = false;
    public Vector2Int gridPos;
    public Image img;
    public Image mask;
    public PuzzleDragHelper helper;

    public Text text;

    [HideInInspector]
    public RectTransform cachedTransform;
    private Vector2 offset;
    private Vector2 enterPos;
    private List<PuzzleDragHelperElement> groupMembers = new List<PuzzleDragHelperElement>();
    private Vector2 localPos;

    private bool InDrag
    {
        set
        {
            if (value == true)
            {
                helper.ElementDragEnter(this);
                cachedTransform.SetAsLastSibling();
            }
            inDrag = value;
        }
        get
        {
            return inDrag;
        }
    }

    private void Awake()
    {
        mask = GetComponent<Image>();
        cachedTransform = this.transform as RectTransform;
    }

    void Start()
    {

    }

    // Update is called once per frame
    void Update()
    {

    }

    public void MoveTarget(Vector2 pos, int id)
    {
        anchorId = id;
        cachedTransform.DOAnchorPos(pos, 0.2f).OnComplete(() => order = cachedTransform.anchoredPosition.x);
    }

    public void Move(Vector2 pos)
    {
        cachedTransform.anchoredPosition = pos;
    }

    public void SetRaycast(bool b)
    {
        mask.raycastTarget = b;
    }

    internal void SetGridPos(Vector2Int pos)
    {
        gridPos = pos;
        float x = (pos - Vector2.one).x * -cachedTransform.sizeDelta.x;
        float y = (pos - Vector2.one).y * cachedTransform.sizeDelta.y;

        (img.transform as RectTransform).anchoredPosition = new Vector2(x, y) + helper.imgOffset;
    }
    public void OnBeginDrag(PointerEventData eventData)
    {
        if (inGrid)
        {
            // 编组长宽都最长后无法移动
            if (groupId != -1 && helper.IsGroupFullRect(groupId))
            {
                OnEndDrag(eventData);
                return;
            }

            InDrag = true;
            offset = cachedTransform.anchoredPosition - localPos;
            groupMembers.Clear();
            // 缓存编组成员
            if (groupId >= 0)
            {
                // 移动组内所有碎片
                foreach (var eid in helper.groupDataSet[groupId])
                {
                    PuzzleDragHelperElement element = helper.elementDataSet[eid];
                    element.cachedTransform.SetAsLastSibling();
                    groupMembers.Add(element);
                }
            }
        }
        else
        {
            helper.scroll.OnBeginDrag(eventData);
        }
        enterPos = eventData.position;
    }


    public void OnDrag(PointerEventData eventData)
    {
        if (inDrag)
        {
            Vector2 localPos;
            RectTransformUtility.ScreenPointToLocalPointInRectangle((cachedTransform.parent as RectTransform), eventData.position, eventData.pressEventCamera, out localPos);
            this.cachedTransform.anchoredPosition = localPos + offset;

            // 移动组内所有碎片
            foreach (var element in groupMembers)
            {
                if (element.id != id)
                {
                    Vector2 pos = element.gridPos - gridPos;
                    element.Move(new Vector2(pos.x * cachedTransform.sizeDelta.x, pos.y * -cachedTransform.sizeDelta.y) + this.cachedTransform.anchoredPosition);
                }
            }

        }
        else
        {
            if (inGrid == false)
            {
                float rad = Mathf.Acos(Vector2.Dot((enterPos - eventData.position).normalized, Vector2.right));
                float ang = rad * Mathf.Rad2Deg;
                ang = ang > 90f ? 180f - ang : ang;
                if (ang >= 20f)
                {
                    if (Mathf.Abs(enterPos.y - eventData.position.y) > 10f)
                    {
                        InDrag = true;
                        helper.scroll.OnEndDrag(eventData);
                        offset = cachedTransform.anchoredPosition - localPos;
                    }
                }
                helper.scroll.OnDrag(eventData);
            }

        }

    }

    public void OnEndDrag(PointerEventData eventData)
    {

        if (inGrid == false)
        {
            helper.scroll.OnEndDrag(eventData);
        }
        helper.ElementDragExit(this);

        InDrag = false;
    }

    public void OnPointerDown(PointerEventData eventData)
    {
        RectTransformUtility.ScreenPointToLocalPointInRectangle((helper.readyContent as RectTransform), eventData.position, eventData.pressEventCamera, out localPos);

    }

    public Vector2Int GetCurrentGridPos()
    {
        if (anchorId != -1)
        {
            return helper.anchorDataSet[anchorId].gridPos;
        }
        return Vector2Int.zero;
    }

}
